library(nycflights13)
library(tidyverse)

flights <- flights

jan1 <- filter(flights, month == 1, day == 1)
jan1

# 5.2.4Exercises ------
# 1.
flights %>% filter(arr_delay == 1 | arr_delay == 2)
# 2.
flights %>% filter(is.na(dep_time)) %>% dim()

flights %>% arrange(desc(arr_delay)) %>% head()

vars <- c("year", "month", "day", "dep_delay", "arr_delay")
flights %>% select(-any_of(vars))

flights %>%
  group_by(dest) %>%
  summarise(
    n = n(),
    distance = mean(distance, na.rm = TRUE),
    delay = mean(arr_delay, na.rm = TRUE)
  ) %>%
  ungroup()

ggplot(aes(distance, delay)) +
  geom_point(aes(size = n), alpha = 1/3) +
  geom_smooth()

popular_dests <- flights %>%
  group_by(dest) %>%
  filter(n() > 365)

popular_dests %>%
  filter(arr_delay > 0) %>%
  mutate(prop_delay = arr_delay / sum(arr_delay)) %>%
  select(year:day, dest, arr_delay, prop_delay) %>%
  ungroup()
